<?php
/**
 * Desc: Service模板
 * User: zdb
 * Date: 2023/6/5
 * Time: 11:22
 */

namespace app\api\service\setting;
use app\util\DateUtil;
use think\facade\Db;

class ClockinReviewRulePhaseService
{

    private $primaryTable;

    public function __construct()
    {
        $this->primaryTable = "yj_clockin_review_rule_phase";
    }

    public function findRowById($id)
    {
        return Db::table($this->primaryTable)->alias('a')
            ->where('id', '=', $id)
            ->strict(false)
            ->find();
    }

    public function findRowByData($data)
    {
        $condition = $this->assembleFilterCondition($data);
        return Db::table($this->primaryTable)->alias('a')
            ->where($condition)
            ->strict(false)
            ->find();
    }

    public function getListByManyId($arrId)
    {
        $list = Db::table($this->primaryTable)->alias('a')
            ->whereIn('id', $arrId)
            ->select()
            ->toArray();
        return $list;
    }

    public function assembleFilterCondition($data)
    {
        if(isset($data['status'])){
            if(is_array($data['status'])){
                $condition = ['a.status '.$data[0].$data[1]];
            }else{
                $condition = ['a.status = '.$data['status']];
            }
        }else{
            $condition = array();
        }

        if (!empty($data['title'])) {
            $condition[] = "title like '%" . $data['title'] . "%'";
        }

        if (!empty($data['clockin_review_rule_id'])) {
            $condition[] = "a.clockin_review_rule_id =".$data['clockin_review_rule_id'];
        }

        if (!empty($data['phase_level'])) {
            $condition[] = "a.phase_level =".$data['phase_level'];
        }

        return empty($condition) ? "" : implode(" AND ", $condition);
    }

    public function getListNoPage($data = [], $limitNum = 30)
    {
        $condition = $this->assembleFilterCondition($data);
        $res = Db::table($this->primaryTable)->alias('a')
            ->where($condition)
            ->limit(0, $limitNum)
            ->order('a.id desc')
            ->select()
            ->toArray();
        return $res;
    }

    public function getListWithPage($data, $pageIndex = 1, $pageSize = 30)
    {
        $offset = ($pageIndex - 1) * $pageSize;
        $condition = $this->assembleFilterCondition($data);
        $res = Db::table($this->primaryTable)->alias('a')
            ->where($condition)
            ->limit($offset, $pageSize)
            ->order('a.id desc')
            ->select();
        return $res;
    }

    public function getTotalNum($data)
    {
        $condition = $this->assembleFilterCondition($data);
        $num = Db::table($this->primaryTable)->alias('a')
            ->where($condition)
            ->count();
        return $num;
    }

    public function createRow($data)
    {
        $data['status'] = isset($data['status']) ? $data['status']:1;
        $data['phase_level'] = $this->getNewPhaseLevel($data['clockin_review_rule_id']);
        if( 0 == $data['is_fix'] ){
            $data['fix_time'] = null;
            $data['interval_day'] = null;
            $durationSecond = 60 * $data['duration'];
            $data['duration_text'] = DateUtil::turnDateDiffToText($durationSecond);
        }else{
            $data['fix_time'] = $data['fix_time'].":00";
        }
        $res = Db::table($this->primaryTable)
            ->strict(false)
            ->insert($data, true);
        return $res;
    }

    public function createManyRow($data)
    {
        $res = Db::table($this->primaryTable)
            ->strict(false)
            ->duplicate($data)
            ->insertAll($data, true);
        return $res;
    }

    public function updateRowById($id, $data)
    {
        $res = Db::table($this->primaryTable)->alias('a')
            ->where('id', '=', $id)
            ->strict(false)
            ->save($data);
        return false !== $res ? true : false;
    }

    public function updateListByData($condition, $data)
    {
        $condition = $this->assembleFilterCondition($data);
        $res = Db::table($this->primaryTable)->alias('a')
            ->where($condition)
            ->save($data);
        return false !== $res ? true : false;
    }

    public function updateListByManyId($arrId, $data)
    {
        $res = Db::table($this->primaryTable)->alias('a')
            ->whereIn('a.id', $arrId)
            ->strict(false)
            ->save($data);
        return false !== $res ? true : false;
    }

    public function removeRowById($id)
    {
        $data = ['status' => 0];
        $res = Db::table($this->primaryTable)
            ->where('id', '=', $id)
            ->strict(false)
            ->save($data);
        return false !== $res ? true : false;
    }

    public function removeListByManyId($arrId)
    {
        $data = ['status' => 0];
        $res = Db::table($this->primaryTable)->alias('a')
            ->whereIn('a.id', $arrId)
            ->strict(false)
            ->save($data);
        return false !== $res ? true : false;
    }

    public function removeListByData($data)
    {
        $condition = $this->assembleFilterCondition($data);
        $data = ['status' => 0];
        $res = Db::table($this->primaryTable)->alias('a')
            ->where($condition)
            ->save($data);
        return false !== $res ? true : false;
    }

    /**
     * 获取新的阶段值
     * @param $data
     */
    public function getNewPhaseLevel($clockinReviewRuleId)
    {
        $nowPhaseLevel = $this->getMaxPhaseLevel($clockinReviewRuleId);
        return $nowPhaseLevel + 1;
    }

    public function getMaxPhaseLevel($clockinReviewRuleId)
    {
        $nowPhaseLevel = Db::table($this->primaryTable)->alias('a')
            ->where('a.clockin_review_rule_id', '=',  $clockinReviewRuleId)
            ->where('a.status', '>', 0)
            ->max('a.phase_level');
        $nowPhaseLevel = empty($nowPhaseLevel) ? 0:$nowPhaseLevel;
        return $nowPhaseLevel;
    }

    /**
     * 获取下一温习规则
     * @param $rulePhaseId
     */
    public function getNextRulePhase($rulePhaseInfo){
        if(!is_array($rulePhaseInfo)){
            $rulePhaseInfo = $this->findRowById($rulePhaseInfo);
        }
        $nextPhaseLevel = $rulePhaseInfo['phase_level'] + 1;
        $ruleId = $rulePhaseInfo['clockin_review_rule_id'];

        $nextCondition = array(
            'clockin_review_rule_id' => $ruleId,
            'phase_level' => $nextPhaseLevel
        );
        $nextPhaseInfo = $this->findRowByData($nextCondition);
        return $nextPhaseInfo;
    }

    public function getFirstRulePhase($rulePhaseInfo)
    {
        if(!is_array($rulePhaseInfo)){
            $rulePhaseInfo = $this->findRowById($rulePhaseInfo);
        }
        $ruleId = $rulePhaseInfo['clockin_review_rule_id'];

        $firstCondition = array(
            'clockin_review_rule_id' => $ruleId,
            'phase_level' => 1
        );
        $firstPhaseInfo = $this->findRowByData($firstCondition);
        return $firstPhaseInfo;
    }

    public function getReviewShowTime($rulePhaseInfo, $specifyTime){
        if(1 == $rulePhaseInfo['is_fix']){
            $addDayNum = $rulePhaseInfo['interval_day'];
            $datePrefix = DateUtil::getCustomDatetime($specifyTime." +".$addDayNum." days", "Y-m-d");
            $showTime = $datePrefix." ".$rulePhaseInfo['fix_time'];
        }else{
            $addMinuteNum = $rulePhaseInfo['duration'];
            $showTime = DateUtil::getCustomDatetime($specifyTime." +".$addMinuteNum." minutes", "Y-m-d H:i:00");
        }
        return $showTime;
    }

}